const db = wx.cloud.database({
  env: 'yun-39dfi'
});
Page({
  data: {
    sumMonney: 0,
    cutMonney: 0,
    note: '',
    max: '20',
    taken: '',
    order_id: '',
    meunnumber:1,
  },
  onLoad: function(options) {
    var meunnumber = this.data.meunnumber
    meunnumber = getApp().globalData.meunnumber
    this.setData({
      order_id: options.order_id, 
      meunnumber : getApp().globalData.meunnumber
    })
    var note = wx.getStorageSync('note')
    var i = this.data.i
    var name = this.data.name
    db.collection('order').doc(options.order_id).get({
      success: (res) => {
        console.log(res.data);
        var foods = res.data.cartList
        console.log("123");
        // 计算总价
        var sum = 0;
        console.log(sum)
        console.log(res.data.promotion.discount)
        console.log("123");
        for (var i in foods) {
          sum += foods[i].price * foods[i].number
        }
        if (sum >= 50 && res.data.promotion.discount > 0) {
          sum -= res.data.promotion.discount
        }
        this.setData({
          order: res.data,
          sumMonney: sum,       
        })
        console.log(999999999)
        console.log(sum)
        console.log(meunnumber)
      }
    })

  },
  // 实时监控textarea值，采用同步的方式存储note值（'key',value）
  listenerTextarea: function(e) {
    var meunnumber = this.data.meunnumber
    var note = e.detail.value;
    wx.setStorageSync('note', note)
  },
  // 点击“去支付”
  gotopay: function(e) {
    // console.log(e)
    // console.log("123456789")
    var meunnumber = this.data.meunnumber
    var sumMonney = this.data.sumMonney
    var order_id = this.data.order_id
    console.log(order_id)
    // console.log(999999999)
    console.log(meunnumber)
    //获取当前时间戳  
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    console.log("当前时间戳为：" + timestamp);

    //获取当前时间  
    var n = timestamp * 1000;
    var date = new Date(n);
    //年  
    var Y = date.getFullYear();
    //月  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //日  
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    //时  
    var h = date.getHours();
    //分  
    var m = date.getMinutes();
    //秒  
    var s = date.getSeconds();

    db.collection('order').doc(order_id).update({
      data: {
        state: "已支付",
        taken:false,
        youhui:1,
        sumMonney: sumMonney,
        promotion: { "name": "满50减10元", "discount": "10" ,"over":true},
        orderinfo: timestamp,
        ordertime: Y +"-"+ M +"-"+ D +"  "+ h + " : " + m + " : " + s,
        ordernum: timestamp,
        meunnumber: 'A' + meunnumber
      },    
      success: res => {
      },
      
    });
    db.collection("record").add({
      data: {
        taken: false,
        ordertime: Y + "-" + M + "-" + D + "  " + h + " : " + m + " : " + s,
        ordernum: timestamp,
        sumMonney: sumMonney,
      },
      success: res => {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000,
          success: function () {
            getApp().globalData.meunnumber = meunnumber+1;
            setTimeout(function () {
              wx.redirectTo({
                url: '../detail/detail?order_id=' + order_id
              })
            })
          }
        })
      },
    });
  }
})